package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.ProductCarouselEntity;
import cn.com.coding.system.service.ProductCarouselService;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author mxz
 */
@RestController
@RequestMapping("cat/productCarouse")
public class ProductCarouselController extends BaseController {

    @Autowired
    private ProductCarouselService productCarouselService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = productCarouselService.queryPage(params);

        return ResT.ok().put("page", page);
    }

    /**
     * 轮播展示
     */
    @RequestMapping("/carousel")
    public ResT all() {
        List<ProductCarouselEntity> carouse = productCarouselService.list(new QueryWrapper<ProductCarouselEntity>().eq("carousel_status", 1));

        return ResT.ok().setData(MapUtil.builder().put("carousel", carouse).build());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {
        ProductCarouselEntity productCarouseInfo = productCarouselService.getById(pid);

        return ResT.ok().put("productCarouseInfo", productCarouseInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody ProductCarouselEntity productCarouseInfo) {
        productCarouselService.save(productCarouseInfo);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody ProductCarouselEntity productCarouseInfo) {
        productCarouselService.updateById(productCarouseInfo);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {
        productCarouselService.removeByIds(Arrays.asList(pids));

        return ResT.ok();
    }

}

